// pages/loan/loan.js
const app = getApp();
const constant = require('../../constant');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    username: '',
    certno: '',
    cardno: '',
    phone: '',
    code: '',
    agreed: false,
    urlPublic: constant.config.url_public
  },

  inputUsername: function (e) {
    this.setData({
      username: e.detail.value
    })
  },
  inputCertno: function (e) {
    this.setData({
      certno: e.detail.value
    })
  },
  inputCardno: function (e) {
    this.setData({
      cardno: e.detail.value
    })
  },
  inputPhone: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },
  inputPhoneCode: function (e) {
    this.setData({
      code: e.detail.value
    })
  },
  checkboxchange: function (e) {
    var isAgree = false;
    if (e.detail.value.length == 1) {
      isAgree = true;
    }
    this.setData({ agreed: isAgree });
  },
  sendSms: function () {
    app.get('User/sendsmsafterlogin', {
      phone: this.data.phone
    }, function (res) {
      wx.showToast({
        title: res.message, icon: 'none', duration: 500
      })
    })
  },
  displayProtocol: function (e) {
    var type = e.currentTarget.dataset.type;
    wx.navigateTo({
      url: '../protocolDisplay/protocolDisplay?type=' + type
    })
  },
  toLoan: function () {
    let that = this;
    if (this.data.agreed == false) {
      wx.showModal({
        title: '提示',
        content: '请阅读并勾选贷款协议',
      })
      return;
    }
    app.get('User/regloan', {
      username: that.data.username,
      certno: that.data.certno,
      cardno: that.data.cardno,
      phone: that.data.phone,
      code: that.data.code
    }, function (res) {
      wx.showToast({
        title: res.message,
        duration: 500,
        success() {
          setTimeout(function () {
            wx.navigateBack({
              delta: 1
            })
          }, 500)
        }
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  chooseImage(){
    wx.chooseImage()
  }
})